#!/usr/bin/python

import os,sys,subprocess

def is_mounted(mount_point):
    try:
        command = ["mount"]
        output = subprocess.Popen(command, stdout=PIPE).communicate()[0]
        
        if output.find(mount_point) > 0:
            return True
        return False
    except:
        raise
        
def bind_mount(src, dst, opt=None):
    try:
        if opt:
            command = " ".join(["mount", "-o", opt, "--bind", src, dst])
        else:
            command = " ".join(["mount", "-i", "--bind", src, dst])
        os.system(command)
    except:
        #print "mount ", src, " ", dst, "failure"
        raise

def bind_mount_i(src, dst):
    try:
        command = " ".join(["mount", "-i", "--bind", src, dst])
        os.system(command)
    except:
        #print "mount ", src, " ", dst, "failure"
        raise      

def mount_tmpfs(mount_point):
    try: 
        command = " ".join(["mount -t tmpfs none", mount_point])
        os.system(command) 
    except:
        raise
    
def mount_aufs(branch, union_point):
    try:
        if not type(branch) == list:
            logging.info("branch not list")
            return
        dirs = "".join(["dirs=", ":".join(branch), " aufs"])
        command = " ".join(["mount -t aufs -o ", dirs, union_point])
        os.system(command)
    except:
        raise 
    
def mount_nfs(url,mount_point):
    try:
        if not os.path.ismount(mount_point):
            command = " ".join(["mount -t nfs -o tcp",url,mount_point])
            os.system(command)
    except:
        raise  
    
def umount(mount_point):
    try:
#        if not os.path.ismount(mount_point):
#            return 
        command = " ".join(["umount",mount_point])
        os.system(command)
    except:
        raise